<?php
/**
 * Author: 式神(vashi)
 * email: 289650682@qq.com
 * Date: 2019/2/21 16:40
 * FileName: Article.php
 */

namespace app\cms\controller;


use app\common\controller\Common;
use app\cms\model\Category;
use app\cms\model\TagsArticle;
use app\cms\model\Position;
use app\cms\model\PostArticle;
use app\cms\model\Tags;

class Article extends Common
{
    protected $middleware = ['Auth'];

    protected function initialize()
    {
        parent::initialize();
        $this->db = new \app\cms\model\Article();
    }
    public function index(){
        $input=input('get.');
        $input['rows'] = isset($input['rows']) ? $input['rows'] : config('rows');
        $input['page'] = isset($input['page']) ? $input['page'] : 1;

        $map = $this->getMap($input);
        $order = $this->getOrder($input);
        $res['total'] = $this->db->where($map)->where('cid',$input['cid'])->order($order)->count();
        $data= $this->db->where($map)->where('cid',$input['cid'])->order($order)->limit($input['rows'])->page($input['page'])->select();

        //加入标签显示
        foreach ($data as $v){
            $ta = TagsArticle::where('article_id',$v['id'])->find();
            $tag_id_arr = explode(",",$ta['tag_ids']);
            $tags = Tags::where('id','in',$tag_id_arr)->column('name');
            $v['tags'] = implode(",",$tags);
        }

        $res['rows'] =$data;
        $res['page'] =$input['page'];
        $d=[
            "code"=>0,
        ];
        $d['data']=$res;
        return json($d);
    }

    public function getone(){
        $get = input('get.');
        $d=[
            "code"=>0,
        ];
        $res = $this->db->find($get['id']);
        $cate = Category::find($res['cid']);
        $res['catname']=$cate['name'];
		
		$posts=[];
		if($res['cid']==0){
			$posts=(new Position())->where('catid',$res['cid'])->select();
		}else{
			$cates = Category::select();
			$cids = getCateId($cates,$res['cid']);
			array_push($cids,$res['cid']);
			$posts=(new Position())->where('catid','in',$cids)->select();
		}
		$posids= (new PostArticle())->where('article_id',$get['id'])->column('post_id');
		
        $d['data']=$res;
		$d['posts']=$posts;
		$d['postid']=$posids;
        return json($d);
    }

    public function gettags(){
        $get = input('get.');
        $d=[
            "code"=>0,
        ];
        $tags = Tags::select();

        $res['all'] =cate_tree($tags);
        $res['checked'] = TagsArticle::where('article_id',$get['article_id'])->find();
        $d['data']=$res;
        return json($d);
    }
    public function settags(){
        $post = input('post.');
        $ta = TagsArticle::where('article_id',$post['article_id'])->find();
        if($ta){
            $post['id'] = $ta['id'];
            $res = (new TagsArticle())->edit($post);
        }else{
            $res = (new TagsArticle())->add($post);
        }
        return json($res);
    }

    public function getcate(){
        $get = input('get.');
        
		$cate = Category::select();
        /*$arrid=[];
        if($get['id']!=0){
            $arrid=getChildsId($cate,$get['id']);
            array_push($arrid,$get['id']);
        }*/
        $arrid=[];
        foreach ($cate as $v){
            array_push($arrid,(string)$v['id']);
        }
        $data=cate_tree($cate);
        //array_unshift($data,['key'=>0,'title'=>'作为顶级栏目','value'=>0]);

		//添加推荐位
		$posts=[];
		$get['cid'] = isset($get['cid'])?$get['cid']:0;
		if($get['cid']==0){
			$posts=(new Position())->where('catid',$get['cid'])->select();
		}else{
			$cids = getCateId($cate,$get['cid']);
			array_push($cids,$get['cid']);
			$posts=(new Position())->where('catid','in',$cids)->select();
		}
		
        $d=[
            "code"=>0,
        ];
        $d['data']=$data;
        $d['arrid']=$arrid;
        $d['posts']=$posts;
        $d['postid']=[];
        return json($d);
    }

    //添加
    public function add(){
		$input = input('post.');
        $res=$this->db->add($input);
		$input['id']=$res['id'];
		(new PostArticle())->add($input);
        return json($res);
    }
    //编辑
    public function edit(){
		$input = input('post.');
        $res=$this->db->edit($input);
		if($input["postbool"]){
			(new PostArticle())->edit($input);
		}
        return json($res);
    }
    //删除
    public function del(){
        //软删除
        $res=$this->db->del(input('post.'));
        return json($res);
    }
    public function move(){
        $input = input('post.');
        $ids = $input['article_id'];
        $arr = [];
        foreach ($ids as $v) {
            $a = [
                'id'=>$v,
                'cid'=>$input['cid']
            ];
            array_push($arr,$a);
        }
        $data = $this->db->saveAll($arr);
        $res=[
            'code'=>0,
            'msg'=>'批量移动栏目成功',
            'data'=>$data
        ];
        return json($res);
    }
}